<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.base.php 476 2014-01-30 16:04:09Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 476 $
* 	zuletzt geaendert von:	$Author: $
* 	Datum:					$Date: 2014-01-30 16:04:09 +0000 (Thu, 30 Jan 2014) $
*
* 	extends by dirk.mueller (edv-mueller.com)
* 	http://www.itdotmedia.de
*
* --------------------------------------------------------------------------------------
* 	based on:
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
* 	Copyright (c) since 2011 SEO:mercari
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
require_once (DIR_FS_INC.'inc.encode_urlsearch.php');
require_once (DIR_FS_INC.'inc.parse_product_options.php');

class base{
  var $group;

  function set_group($group) { $this->group = $group; }

  function parse_parameters($parameter) {
    $items = array();
    if (strlen($parameter) > 0) {
      foreach(explode("|", $parameter) as $val) {
        list($k, $v) = explode("=", $val);
        $v = strtr($v, array('##' => '='));

        if ((strpos($k, "[") === 0)) {
          if (strpos($k, "[".$_SESSION['languages_id']."]") === 0) {
            $items[substr($k, strpos($k, "]")+1)] = $v;
          }
        }

        else {
          $items[$k] = $v;
        }
      }
    }
    return $items;
  }

  function get_template_name() {
    $tmpl_parts = explode('_', $this->group['type']);
    while(count($tmpl_parts) > 0) {
      $file_name = DIR_FS_DOCUMENT_ROOT. 'templates/'.CURRENT_TEMPLATE.'/module/product_konfigurator/'.join('_', $tmpl_parts).'.html';
      if(file_exists($file_name))
        return join('_', $tmpl_parts).'.html';

      array_pop($tmpl_parts);
    }
    return false;
  }

  function get_option_smarty($product_id) {
    global $db;

    $option_smarty = new Smarty();
    if (!$this->assign_template_base($option_smarty, $product_id)) return false;

    $sql = "
      select
      	o.konfigurator_options_id, o.konfigurator_options_name,
      	o.konfigurator_options_parameter,
      	po.is_default
      from
      	".TABLE_KONFIGURATOR_PRODUCT_OPTION." po
      	inner join ".TABLE_KONFIGURATOR_OPTIONS." o
      		on po.konfigurator_option = o.konfigurator_options_id
      where
      	po.product_id = '".$product_id."'
      	and po.konfigurator_option_group = '".$this->group['id']."'
      	and po.konfigurator_option != 'HAVE_TO'
      order by
      	o.konfigurator_options_sort";
    $query = $db->db_query($sql, true);

    $options = array();
    $std_value = '';
		if($query->_numOfRows > 0){
			while(!$query->EOF){
        $p = $this->parse_parameters($query->fields['konfigurator_options_parameter']);
        $option = array(
          'ID'    => $query->fields['konfigurator_options_id'],
          'NAME'  => $query->fields['konfigurator_options_name'],
          'STD'   => ($query->fields['is_default'] == 'true')
        );
        foreach($p as $k => $v) $option[strtoupper($k)] = $v;
        $options[] = $option;
        if ($query->fields['is_default'] == 'true') $std_value = $query->fields['konfigurator_options_id'];

        $query->moveNext();
      }
    }
    $option_smarty->assign('OPTIONS', $options);
    if (isset($_GET['p']) && ($p = encode_urlsearch($_GET['p'])) != '') {
    	$parameter = parse_product_options($p);
    	if(isset($parameter[$this->group['id']]))
	      $option_smarty->assign('LOAD_VALUE', $parameter[$this->group['id']]);
    }
    else if ($std_value!='')
      $option_smarty->assign('LOAD_VALUE', $std_value);

  	$option_smarty->assign('language', $_SESSION['language']);
  	
  	return $option_smarty;
  }

  function assign_template(&$smarty, $product_id, $with_form_action) {
    $option_smarty = $this->get_option_smarty($product_id);
    if ($option_smarty == false) return;
    $template_name = $option_smarty->getTemplateVars('TEMPLATE');
    $option_smarty->assign('FORM_ACTION', $with_form_action);

  	if (!CacheCheck()) {
      $option_smarty->caching = false;
      $option_box = $option_smarty->fetch(CURRENT_TEMPLATE.'/module/product_konfigurator/'.$template_name);
    } else {
      $option_smarty->caching = true;
      $option_smarty->cache_lifetime = CACHE_LIFETIME;
      $option_smarty->cache_modified_check = CACHE_CHECK;
      $cache_id = $product->data['products_id'].$this->group['id'].$_SESSION['language'].$_SESSION['customers_status']['customers_status_name'];
  		$option_box = $option_smarty->fetch(CURRENT_TEMPLATE.'/module/product_konfigurator/'.$template_name, $cache_id);
    }
    
    $MODULE_product_options = $smarty->getTemplateVars('MODULE_product_options');
    if ($MODULE_product_options != '') $MODULE_product_options .= "\n";
    $smarty->assign('MODULE_product_options', $MODULE_product_options . $option_box);
  }
  
  
  function assign_template_base(&$option_smarty, $product_id) {

    $option_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
    $option_smarty->assign('ID', $this->group['id']);
    $option_smarty->assign('NAME', $this->group['name']);

    if (($template_name = $this->get_template_name()) === false) return false;
    $option_smarty->assign('TEMPLATE', $template_name);

    return true;
  }

  function print_js($product_id, $with_form_action) {
    $file_name = CURRENT_TEMPLATE.'/module/product_konfigurator/js.'.strtolower($this->group['id']).'.js';
    if(file_exists(DIR_FS_DOCUMENT_ROOT. 'templates/'. $file_name)) {
      $js_smarty = new Smarty();

      if (($template_name = $this->get_template_name()) !== false)
        $js_smarty->assign('TEMPLATE', $template_name);
      else
        $js_smarty->assign('TEMPLATE', '-');

      $js_smarty->assign('FORM_ACTION', $with_form_action);

    	if (!CacheCheck()) {
        $js_smarty->caching = false;
        $js_box = $js_smarty->fetch($file_name);
      } else {
        $js_smarty->caching = true;
        $js_smarty->cache_lifetime = CACHE_LIFETIME;
        $js_smarty->cache_modified_check = CACHE_CHECK;
        $cache_id = $_POST['pID'].$this->group['id'].'js'.$_SESSION['language'].$_SESSION['customers_status']['customers_status_name'];
    		$js_box = $js_smarty->fetch($file_name, $cache_id);
      }

      echo $js_box;
    }
  }

  function print_js_base($product_id, $with_form_action) {
    $file_name = CURRENT_TEMPLATE.'/module/product_konfigurator/js.base.'.strtolower($this->group['type']).'.js';
    if(file_exists(DIR_FS_DOCUMENT_ROOT. 'templates/'. $file_name)) {
      $js_smarty = new Smarty();
      $js_smarty->assign('FORM_ACTION', $with_form_action);

    	if (!CacheCheck()) {
        $js_smarty->caching = false;
        $js_box = $js_smarty->fetch($file_name);
      } else {
        $js_smarty->caching = true;
        $js_smarty->cache_liftime = CACHE_LIFETIME;
        $js_smarty->cache_modified_check = CACHE_CHECK;
        $cache_id = $this->group['type'].'js'.$_SESSION['language'].$_SESSION['customers_status']['customers_status_name'];
    		$js_box = $js_smarty->fetch($file_name, $cache_id);
      }

      echo $js_box;
    }

  }
  
  function _calculate_price(&$price, $price_value) {
    if (KONFIGURATOR_PRICE_IS_BRUTTO === 'true') {
      $price['brutto'] = $price_value;
      $price['netto'] = $price_value / (1.0 + $price['tax']['rate']/100.0);
    }
    else {
      $price['netto'] = $price_value;
      $price['brutto'] = $price_value * (1.0 + $price['tax']['rate']/100.0);
    }
    $price['tax']['value'] = $price['brutto'] - $price['netto'];
    
    return $price;
  }
  
  function get_price($product_id, $price, $post) {
    global $db;
    $price_value = 0;

    if (!isset($post[$this->group['id']]) || $post[$this->group['id']] == '' ) return $price;

    $sql = "
      select
        product_id, price
      from
        ".TABLE_KONFIGURATOR_PRODUCT_OPTION." po
      where
        po.product_id = '".$product_id."'
        and konfigurator_option_group = '".$this->group['id']."'
        and konfigurator_option = '".$post[$this->group['id']]."'";
    $query = $db->db_query($sql, true);
		if($query->_numOfRows > 0){
      $price_value = $query->fields['price'];
      if ($price_value == null) $price_value = 0;
    }
    
    $this->_calculate_price($price, $price_value);
    return $price;
  }
  
  function get_description($product_id, $post) {
    global $db;
    $description = array('option_name' => '', 'option_value' => '');

    if (!isset($post[$this->group['id']]) || $post[$this->group['id']] == '' ) return $description;

    $sql = "
      select
      	po.product_id,
      	og.konfigurator_optiongroups_name, o.konfigurator_options_name
      from
      	".TABLE_KONFIGURATOR_PRODUCT_OPTION." po
      	inner join ".TABLE_KONFIGURATOR_OPTIONGROUPS." og
      		on og.konfigurator_optiongroups_id = po.konfigurator_option_group
      	inner join ".TABLE_KONFIGURATOR_OPTIONS." o
      		on o.konfigurator_optiongroups_id = po.konfigurator_option_group
      		and o.konfigurator_options_id = po.konfigurator_option
      where
        po.product_id = '".$product_id."'
        and konfigurator_option_group = '".$this->group['id']."'
        and konfigurator_option = '".$post[$this->group['id']]."'";

    $query = $db->db_query($sql, true);
		if($query->_numOfRows > 0) {
      return array(
        'option_name' => $query->fields['konfigurator_optiongroups_name'],
        'option_value' => $query->fields['konfigurator_options_name']
      );
    }

    return $description;
  }

}